<?php
namespace Common\Model;

class CostServerModel extends Model {
    protected $tableName = 'public.cost_server';

    public function save2($d) {
        $id = empty($d['id']) ? null : (int)$d['id'];

        if(empty($id) && isset($d['id'])) {
            unset($d['id']);
        }

        $action = empty($d['id']) ? 'add' : 'save';

        if(empty($d['slave_ip'])) {
            throw new Exception('请指定服务器IP');
        }

        if($this->checkSlaveIpExists($d['slave_ip'], $id)) {
            throw new Exception($d['slave_ip'] . '已经存在');
        }

        $d['status'] = (int)$d['status'];

        if($action == 'save') {
            if(!$this->getById($d['id'])) {
                throw new Exception('没有找到相应服务器数据');
            }
        }

        return $this->$action($d);
    }

    /**
     * checkSlaveIpExists
     * 检测IP是否已存在
     *
     * @param mixed $slaveIp
     * @param mixed $id
     * @return void
     */
    public function checkSlaveIpExists($slaveIp, $id = null) {
        $id   = empty($id) ? null : (int)$id;
        $expr = array('slave_ip' => $slaveIp);

        if(!empty($id)) {
            $expr['id'] = array('NEQ', $id);
        }

        return $this->where($expr)->find();
    }

    /**
     * buildSearchExpr 
     * 组装查询条件
     * 
     * @param mixed $d  查询数据，一般来源于$_GET或$_POST
     * @param mixed $field  可查询的字段对照表，其结构为array(array('表单项目名称' => '数据库字段名称'))
     * @access public
     * @return void 返回值为数组，参见方法内部注释
     */
    public function buildSearchExpr($d, $field = NULL) {
        $expr = array();
        $dao  = array();
        $sortExpr = 'id DESC';

        /**
         * 下标为表单中的项目名称，值为数据库中表的字段名称 
         */
        if(empty($field)) {
            $field = array(
                'slave_ip'      => 'slave_ip',
            );
        }

        foreach($field as $k => $v) {
            $tmpExpr = null;
            $val     = isset($d[$k]) ? trim($d[$k]) : '';

            if(strlen($val) < 1) {
                continue;
            }

            switch($k) {
                default:
                    $expr[$v] = $d[$k];
            }

            if(!empty($tmpExpr)) {
                $expr['_string'] = empty($expr['_string'])
                                   ? $tmpExpr
                                   : $expr['_string'] . " AND {$tmpExpr}";
            }

            $dao[$k] = $d[$k];
        }

        if(!empty($d['sort']) && in_array($d['sort'], $this->getDbFields())) {
            $sortdir = empty($d['sortdir'])                     ? 'DESC'   : strtoupper($d['sortdir']);
            $sortdir = in_array($sortdir, array('DESC', 'ASC')) ? $sortdir : 'DESC';

            $sort        = $d['sort'];
            $dao['sort'] = $sort;

            $sortExpr       = "{$sort} {$sortdir}";
            $dao['sortdir'] = $sortdir;
        }

        return array(
            'expr' => $expr,            //ThinkPHP查询条件表达式
            'sort' => $sortExpr,        //排序表达式
            'json' => json_encode($dao) //用于回填至查询表单的数据
        );
    }

}
